﻿/*
 * Created by SharpDevelop.
 * User: tentus
 * Date: 2006-10-19
 * Time: 22:54
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.Collections.Generic;

namespace ActionManager
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	public partial class ActionList
	{
		private ActionManagerXml xmlManager;				
		private string xml;
		
		public ActionList()
		{
			InitializeComponent();
			
			xmlManager = new ActionManagerXml();
			xml = "";
		}				
				
		
		private void LoadActionListForm()
		{
			int indx = listBox1.SelectedIndex;
			listBox1.Items.Clear();								
			
			List<string> list = new List<string>();
			xmlManager.GetActionList(xml, list);
			
			for (int i=0; i<list.Count; ++i)
				listBox1.Items.Add(list[i]);
			
			if (indx<listBox1.Items.Count)
			  listBox1.SelectedIndex = indx;
		}				
		
		int GetFrequencyIndex(string _frequency)
		{
			if (_frequency=="Once")return 0;
			
			if (_frequency=="Daily")return 1;
						
			if (_frequency=="Weekly")return 2;
			
			if (_frequency=="Workdays")return 3;
			
			if (_frequency=="Monthly")return 4;
			
			if (_frequency=="Yearly")return 5;
			
			return 0;
		}
		
		private void LoadActionType()
		{					
			string date = "", time = "", shortcut = "", frequency = "", type = "";
			xmlManager.GetActionType(xml, ref shortcut, ref date, ref time, ref frequency, ref type);
			
			textBox2.Text = shortcut;
			dateTimePicker1.Text = date;
			dateTimePicker2.Text = time;
			comboBox1.SelectedIndex = GetFrequencyIndex(frequency);
			
			if (type=="Shortcut")
				radioButton1.Checked = true;
			else
				radioButton2.Checked = true;
			
			EnableRadioComponents();
		}
		
		private void RefreshForm()
		{			
			textBox1.Text = xmlManager.GetGroupName(xml);
			
			LoadActionType();
			
			LoadActionListForm();						
		}			
		
		private string GetFrequencyForm()
		{
			string freq = "Once";
			
			switch (comboBox1.SelectedIndex)
			{
				case 0: freq = "Once";
				        break;
				case 1: freq = "Daily";
				        break;
				case 2: freq = "Weekly";
				        break;
				case 3: freq = "Workdays";
				        break;
				case 4: freq = "Monthly";
				        break;
				case 5: freq = "Yearly";
				        break;        
			}
			
			return freq;
		}
		
		private string GetTypeForm()
		{
			string xml_type = "Shortcut";
			if (radioButton2.Checked)
				xml_type = "Schedule";
			
			return xml_type;						
		}
		
		private string GetXmlForm()
		{						
			xml = xmlManager.SetGroupName(xml, textBox1.Text);
			xml = xmlManager.SetActionType(xml, textBox2.Text, dateTimePicker1.Text, dateTimePicker2.Text, GetFrequencyForm(), GetTypeForm());
			
			return xml;
		}
		
		public string ShowForm(string _xml)
		{
			string old_xml = _xml;
			
			if (_xml!="")
	    		xml = _xml;					
			else
				xml = xmlManager.CreateDefaultGroup("Nazwa grupy", "", "Once", "Shortcut");							
			
			RefreshForm();
			
			if (ShowDialog()==DialogResult.OK)
			   return GetXmlForm();									
			
			return old_xml;
		}
		
		void TextBox2KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			string tekst = "";
			
			if (e.Control)
			  tekst+="Ctrl";
			
			if (e.Alt)
			{
				if (tekst.Length>0)tekst+="+";
				
				tekst+="Alt";
			}
			
			if (e.Shift)
			{
				if (tekst.Length>0)tekst+="+";
				
				tekst+="Shift";
			}
			
			if (tekst.Length>0)tekst+="+";
			
			if (e.KeyValue>32)
				tekst += e.KeyCode;			
						
			textBox2.Text = tekst;
		}
		
		private ActionDll.SFilenameItem GetActionFilename()
		{
			ActionDll actionDll = new ActionDll();
			
			List<ActionDll.SFilenameItem> list = new List<ActionDll.SFilenameItem>();
			actionDll.GetActionList(list);
			
			List<string> lista_text = new List<string>();
			for (int i=0; i<list.Count; ++i)
				lista_text.Add(list[i].tekst);
			
			ActionComboList actionComboList = new ActionComboList();
			int indx = actionComboList.ShowComboList(lista_text);
			if (indx<0)return new ActionDll.SFilenameItem("", "");
			
			return list[indx];
		}
		
		private void AddActionForm()
		{
			
			ActionDll.SFilenameItem FilenameItem = GetActionFilename();
			if (FilenameItem.filename=="")return;
			
			ActionDll actionDll = new ActionDll();		
			string settings = "";
			if (actionDll.GetActionSettings(FilenameItem.filename, ref settings)==false)return;
			
			string xml_action = xmlManager.getActionXml(FilenameItem.filename, FilenameItem.tekst, settings);
			
			xml = xmlManager.AddAction(xml, xml_action);	
		}
		
		private void EditActionForm()
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;
			
			string xml_action = xmlManager.GetAction(xml, listBox1.SelectedIndex);
			xml_action = xmlManager.EditActionDll(xml_action);
			
			xml = xmlManager.EditAction(xml, xml_action, listBox1.SelectedIndex);	
		}
				
		
		void Button3Click(object sender, System.EventArgs e)
		{						
			AddActionForm();
			
			LoadActionListForm();
		}
		
		void Button5Click(object sender, System.EventArgs e)
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;						
			
			xml = xmlManager.DeleteAction(xml, listBox1.SelectedIndex);
			
			LoadActionListForm();
		}
		
		void Button4Click(object sender, System.EventArgs e)
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;
						
			EditActionForm();
			
			LoadActionListForm();
		}
		
		void Button6Click(object sender, System.EventArgs e)
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;						
			
			xml = xmlManager.MoveUpAction(xml, listBox1.SelectedIndex);
			
			listBox1.SelectedIndex--;
			
			LoadActionListForm();
		}
		
		void Button7Click(object sender, System.EventArgs e)
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;						
			
			xml = xmlManager.MoveDownAction(xml, listBox1.SelectedIndex);
			
			listBox1.SelectedIndex++;
			
			LoadActionListForm();			
		}
				
		private void EnableRadioComponents()
		{
			textBox2.Enabled        = radioButton1.Checked;
			dateTimePicker1.Enabled = !radioButton1.Checked;
			dateTimePicker2.Enabled = !radioButton1.Checked;
			comboBox1.Enabled       = !radioButton1.Checked;
			label3.Enabled          = !radioButton1.Checked;
		}
		
		void RadioButton1Click(object sender, System.EventArgs e)
		{
			EnableRadioComponents();
		}
				
		
		void ListBox1MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;
						
			EditActionForm();
			
			LoadActionListForm();
		}
	}
}
